package Performance_Selection_Sort;

/**
 * @author wly
 * @version V1.0.0
 * @date 2023/1/21 14:12
 */
public class SortingHelper {
    private SortingHelper(){}

    // 判断数组arr是否有序
    public static <E extends Comparable<E>> boolean isSorted(E[] arr){
        for (int i = 1; i < arr.length; i++) {
            if (arr[i - 1].compareTo(arr[i]) > 0){
                return false;
            }
        }
        return true;
    }

    // 测试算法性能表现
    public static <E extends Comparable<E>> void sortTest(String sortName, E[] arr){
        long startTime = System.nanoTime();
        if (sortName.equals("SelectionSort")){
            SelectionSort.sort02(arr);
        }
        long endTime = System.nanoTime();

        double time = (endTime - startTime) / 1000000000.0;

        if (!SortingHelper.isSorted(arr)){
            throw new RuntimeException(sortName + " failed!");
        }
        System.out.println(String.format("%s, n = %d : %f s", sortName, arr.length, time));
    }
}
